package com.standstill.auth;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Guo Qiao on 2017/11/27.
 */

public class MyZuulFilter extends ZuulFilter {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();

        String session = request.getParameter("sid");

        if(session != null && session.equals("123")){
            logger.info("登录成功！");
            return null;
        }

        logger.info("登录失败：" + session);

        requestContext.setSendZuulResponse(false);
        requestContext.setResponseStatusCode(401);

        return null;
    }
}
